/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbd.crawler.dp;

import bbd.crawler.dp.fwk.ProcessorInterface;
import bbd.crawler.util.Console;
import java.util.HashMap;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

// TODO : return a value object to caller. no data returned to caller currently

/**
 *
 * @author Amogh
 */
public class WikipediaProcessor implements ProcessorInterface {
    
    private static final String REGEX_EXTRACT_INFOBOX =
                                        "[{]{2}Infobox (M|m)usical[ . \\w \\W \\s \\S \\d \\D \\n ]*\\}}[\\s]*'''[\\w \\W \\s]+'''";
    private static final String REGEX_REMOVE_WIKIQUOTE   = "\\{\\{Wikiquote\\}\\}";
    private static final String REGEX_REMOVE_ARTIST_NAME = "'''(.*)$";
    private static final String REGEX_REMOVE_BRACES      = "[ { } \\[ \\] ]";
    private static final String REGEX_SPLIT              = "[|][\\s]+";

    private static final String REPLACE_WITH_NOTHING = "";
    private static final String REPLACE_WITH_SPACE   = " ";

    private boolean dataProcessed = false;

    private String infobox = null;

    private HashMap dataMap = null;

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public WikipediaProcessor () { } // end default constructor

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public void process (Object data) {

        String parsedData = (String) data;

        parsedData = parsedData.replaceAll ( "\\n", REPLACE_WITH_NOTHING );

        try {

            Pattern pattern = Pattern.compile ( REGEX_EXTRACT_INFOBOX );

            Matcher matcher = pattern.matcher ( parsedData );

            if ( matcher.find() ) {

                infobox = matcher.group();

                if ( infobox != null ) {

                    processHelper();
                }
                else {
                    
                    Console.debug ( "WikipediaProcessor.process", "Infobox is null" );
                }
            }
            else {

                Console.debug ( "WikipediaProcessor.process", "Regex found NO INFOBOX ! :O - What's there then?" );
                Console.debug ( "WikipediaProcessor.process", parsedData );
            }
        }
        catch ( Exception ex ) { ex.printStackTrace(); }

    } // end function process

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public Object getProcessedData () {
        return dataMap;
    } // end function getProcessedData

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public boolean isDataProcessed() {
        return dataProcessed;
    } // end method isDataProcessed

    //--------------------------------------------------------------------------
    /** Javadoc here */
    private void processHelper () {

        infobox = infobox.replaceAll (
                                REGEX_REMOVE_WIKIQUOTE,
                                REPLACE_WITH_NOTHING
                          );

        infobox = infobox.replaceAll (
                                REGEX_REMOVE_ARTIST_NAME,
                                REPLACE_WITH_NOTHING
                          );

        infobox = infobox.replaceAll (
                                REGEX_REMOVE_BRACES,
                                REPLACE_WITH_SPACE
                          );

        String [] keyValuePair = null;
        String [] keyValueList = infobox.split ( REGEX_SPLIT );

        int statInserted = 0; // to print statistics
        int statIgnored  = 0;

        String statIgnoredKeys = new String ( "| " );

        for ( int i = 0; i < keyValueList.length; i++ ) {

            if ( keyValueList[i].contains ("=") ) {

                keyValuePair = keyValueList[i].split ("=");

                if ( keyValuePair.length > 1 ) {

                    if ( keyValuePair[1].trim().length() > 1 ) {

                        if ( dataMap == null ) {
                            dataMap = new HashMap();
                        }

                        dataMap.put (
                                    keyValuePair[0].trim(),
                                    keyValuePair[1].trim()
                                );

                        statInserted ++;
                        
                        /*
                        Console.debug (
                                    "WikipediaProcessor.processHelper",
                                    "\t" + keyValuePair[0].trim() + " = " + keyValuePair[1].trim()
                                 );
                        */
                    }
                    else {

                        statIgnored ++;

                        statIgnoredKeys = statIgnoredKeys.concat ( keyValuePair[0].trim() + " | " );

                        /*
                        Console.debug (
                                    "WikipediaProcessor.processHelper",
                                    "\t ** NO [VALUE] FOR [KEY] - " + keyValuePair[0].trim()
                                 );
                        */
                    }
                }
                else {

                    Console.debug (
                                "WikipediaProcessor.processHelper",
                                "\t ** INVALID SPLIT FOR - " + keyValuePair[0].trim() + " | length - " + keyValuePair.length
                             );
                }
            }
            else {

                Console.debug (
                            "WikipediaProcessor.processHelper",
                            "\t ** UNKNOWN VALUE LIST - " + keyValueList[i].trim()
                         );
            }
        }

        dataProcessed = true;

        Console.debug (
                    "WikipediaProcessor.processHelper",
                    "Key Value List length - " + keyValueList.length
                 );

        if ( dataMap != null && statInserted > 0 ) {

            Console.debug (
                        "WikipediaProcessor.processHelper",
                        "Key Value(s) inserted - " + statInserted + " | keys - " + dataMap.keySet()
                     );
        }

        Console.debug (
                    "WikipediaProcessor.processHelper",
                    "Key Value(s) ignored  - " + statIgnored + " | keys - " + statIgnoredKeys
                 );

    } // end function processHelper

} // end class WikipediaProcessor
